﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class MeetingEx : PageBase
    {
        public string MeetingContent { get; set; }
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
            this.lbWorkPoint.Text = ModuleUtil.GetWorkPointConent(this.CurrentStepID);
            SetPage();
        }

        private void LoadData()
        {

            var meet = DB.partyJoinMeetingExs.Find(this.CurrentStandID);
            if (meet != null)
            {
                this.dtMeetingTime.Text = meet.MeetingTime?.ToString("yyyy-MM-dd");
                this.MeetingContent = meet.MeetingContent;

                this.txtAllMember.Text = meet.AllMember;
                this.txtMemberShouldBeTo.Text = meet.MemberShouldBeTo;
                this.txtMemberIsTo.Text = meet.MemberIsTo;
                this.txtMemberAgree.Text = meet.MemberAgree;
                this.txtMemberWaiver.Text = meet.MemberWaiver;
                this.txtMemberDisAgree.Text = meet.MemberDisAgree;
                this.ddlMemberState.SelectedValue = meet.MemberState.ToString();

                this.txtAllPeople.Text = meet.AllPeople;
                this.txtPeopleShouldBeTo.Text = meet.PeopleShouldBeTo;
                this.txtPeopleIsTo.Text = meet.PeopleIsTo;
                this.txtPeopleAgree.Text = meet.PeopleAgree;
                this.txtPeopleWaiver.Text = meet.PeopleWaiver;
                this.txtPeopleDisAgree.Text = meet.PeopleDisAgree;
                this.ddlPeopleState.SelectedValue = meet.PeopleState.ToString();
            }
            else
                dtMeetingTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        private void SaveData(bool isClose, bool IsSubmit = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过
            bool bResult = true;
            PartyJoinLinkExample oModel = null;
            oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            var meet = DB.partyJoinMeetingExs.Find(this.CurrentStandID);
            if (meet == null)
                meet = new partyJoinMeetingEx();

            meet.MeetingTime = Convert.ToDateTime(this.dtMeetingTime.Text);
            meet.MeetingContent = this.Request.Form["editorValue"];
            meet.AllMember = this.txtAllMember.Text;
            meet.MemberShouldBeTo = this.txtMemberShouldBeTo.Text;
            meet.MemberIsTo = this.txtMemberIsTo.Text;
            meet.MemberAgree = this.txtMemberAgree.Text;
            meet.MemberWaiver = this.txtMemberWaiver.Text;
            meet.MemberDisAgree = this.txtMemberDisAgree.Text;
            meet.MemberState = Convert.ToInt32(this.ddlMemberState.SelectedValue);

            meet.AllPeople = this.txtAllPeople.Text;
            meet.PeopleShouldBeTo = this.txtPeopleShouldBeTo.Text;
            meet.PeopleIsTo = this.txtPeopleIsTo.Text;
            meet.PeopleAgree = this.txtPeopleAgree.Text;
            meet.PeopleWaiver = this.txtPeopleWaiver.Text;
            meet.PeopleDisAgree = this.txtPeopleDisAgree.Text;
            meet.PeopleState = Convert.ToInt32(this.ddlPeopleState.SelectedValue);
            meet.StepID = this.CurrentStepID;
            meet.AppllicationID = this.AppllicationID;
            if (string.IsNullOrEmpty(meet.StandID))
            {
                meet.StandID = this.CurrentStandID;

                meet.CreateTime = DateTime.Now;
                DB.partyJoinMeetingExs.Add(meet);
                DB.SaveChanges();
            }
            else
                DB.SaveChanges();
            if (IsSubmit)
            {
                if (this.ddlMemberState.SelectedValue == "0")
                {
                    oModel.HandleUserID = this.CurrentUser.ID;
                    oModel.HandleUserName = this.CurrentUser.UserName;
                    oModel.HandleTime = DateTime.Now;
                    oModel.Status = (int)Enum_FlowStanceStatus.通过;
                    bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                }
            }
        }


        #region 设置显示隐藏
        private void SetPage()
        {
            switch (this.CurrentStepID)
            {
                case ProcesUtil.JoinStep22:
                case ProcesUtil.JoinStep26:
                case ProcesUtil.JoinStep35:
                    //this.heMeetingContent.Height = 300;
                    this.panel5.Hidden = this.panel6.Hidden = this.panel7.Hidden = true;
                    break;
            }
            if (this.ReadOnly == "1")
            {
                this.btnSaveClose.Hidden = true;
                this.btnSubmit.Hidden = true;
            }
        }
        #endregion

        public bool CheckValue()
        {
            bool ret = true;
            int intParse = 0;
            if (this.txtAllPeople.Text.Trim() == "" || !Int32.TryParse(this.txtAllPeople.Text, out intParse))
            {
                this.txtAllPeople.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtPeopleShouldBeTo.Text.Trim() == "" || !Int32.TryParse(this.txtPeopleShouldBeTo.Text, out intParse))
            {
                this.txtPeopleShouldBeTo.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtPeopleIsTo.Text.Trim() == "" || !Int32.TryParse(this.txtPeopleIsTo.Text, out intParse))
            {
                this.txtPeopleIsTo.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtPeopleAgree.Text.Trim() == "" || !Int32.TryParse(this.txtPeopleAgree.Text, out intParse))
            {
                this.txtPeopleAgree.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtPeopleWaiver.Text.Trim() == "" || !Int32.TryParse(this.txtPeopleWaiver.Text, out intParse))
            {
                this.txtPeopleWaiver.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtPeopleDisAgree.Text.Trim() == "" || !Int32.TryParse(this.txtPeopleDisAgree.Text, out intParse))
            {
                this.txtPeopleDisAgree.MarkInvalid("请输入有效值");
                ret = false;
            }
            switch (this.CurrentStepID)
            {
                case ProcesUtil.JoinStep22:
                case ProcesUtil.JoinStep26:
                case ProcesUtil.JoinStep35:
                    ret = true;
                    break;
            }

            if (this.txtAllMember.Text.Trim() == "" || !Int32.TryParse(this.txtAllMember.Text, out intParse))
            {
                this.txtAllMember.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtMemberShouldBeTo.Text.Trim() == "" || !Int32.TryParse(this.txtMemberShouldBeTo.Text, out intParse))
            {
                this.txtMemberShouldBeTo.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtMemberIsTo.Text.Trim() == "" || !Int32.TryParse(this.txtMemberIsTo.Text, out intParse))
            {
                this.txtMemberIsTo.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtMemberAgree.Text.Trim() == "" || !Int32.TryParse(this.txtMemberAgree.Text, out intParse))
            {
                this.txtMemberAgree.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtMemberWaiver.Text.Trim() == "" || !Int32.TryParse(this.txtMemberWaiver.Text, out intParse))
            {
                this.txtMemberWaiver.MarkInvalid("请输入有效值");
                ret = false;
            }
            if (this.txtMemberDisAgree.Text.Trim() == "" || !Int32.TryParse(this.txtMemberDisAgree.Text, out intParse))
            {
                this.txtMemberDisAgree.MarkInvalid("请输入有效值");
                ret = false;
            }

            if (string.IsNullOrEmpty(FilterSpecialCharacters.Filter(this.Request.Form["editorValue"] ?? "")))
            {
                Alert.Show(string.Format("{0}不能为空!", this.lbMeetingContent.Text), String.Empty);
                ret = false;
            }

            return ret;
        }

        #region 按钮
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckValue())
            {
                this.SaveData(true, true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (CheckValue())
            {
                SaveData(false);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        #endregion
    }
}